package com.example.demo2;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class demo2 {

    // 爬取一个页面关键词
    public static List<String> scrapeKeywords(String url) throws IOException {
        List<String> keywords = new ArrayList<>();
        Document doc = Jsoup.connect(url).get();
        Elements elements = doc.select("table.keyword-table td a");
        for (Element el : elements) {
            keywords.add(el.text());
        }
        return keywords;
    }


    public static void main(String[] args) {
        String baseUrl = "https://www.fe-siken.com/s/keyword/";

        // 50音基本行
        char[] rows = {'a', 'i', 'u', 'e', 'o'};
        char[] consonants = {'a', 'k', 's', 't', 'n', 'h', 'm', 'y', 'r', 'w'};

        List<String> allKeywords = new ArrayList<>();
        int totalPages = 0;

        try {
            for (char c1 : consonants) {
                for (char c2 : rows) {
                    String page = "" + c1 + c2 + ".html";
                    String url = baseUrl + page;
                    System.out.println("抓取页面: " + url);

                    try {
                        List<String> keywords = scrapeKeywords(url);
                        if (!keywords.isEmpty()) {
                            allKeywords.addAll(keywords);
                            totalPages++;
                        }
                    } catch (IOException e) {
                        // 页面不存在或连接失败，忽略继续
                        System.out.println("页面不存在或无法连接: " + url);
                    }
                }
            }

            // 写Excel
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet("FEキーワード");

            Row header = sheet.createRow(0);
            header.createCell(0).setCellValue("番号");
            header.createCell(1).setCellValue("キーワード");

            for (int i = 0; i < allKeywords.size(); i++) {
                Row row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(i + 1);
                row.createCell(1).setCellValue(allKeywords.get(i));
            }

            String outputFile = "FE_Keywords_Full_50on.xlsx";
            try (FileOutputStream fos = new FileOutputStream(outputFile)) {
                workbook.write(fos);
            }
            workbook.close();

            System.out.println("抓取完成，共抓取 " + totalPages + " 个页面，关键词总数: " + allKeywords.size());
            System.out.println("生成文件: " + outputFile);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
